package com.regretbuying.front.controller;

import com.alibaba.fastjson.JSON;
import com.regretbuying.front.service.goods.GoodsService;
import com.regretbuying.front.service.shopping_cart.CartService;
import com.regretbuying.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Resource(name="fcasi")
    private CartService cartService;

    @Resource(name="fgsi")
    private GoodsService goodsService;

    //优惠卷
    @RequestMapping(value = "/cartCoupons", produces = {"json/html;charset=UTF-8"})
    @ResponseBody
    public String CartCouponsById(@RequestParam(defaultValue = "")String couponsId, HttpSession session){
        Coupons coupons = cartService.getCouponsById(couponsId);

        Map resultMap = new HashMap();
        if(coupons != null){
            resultMap.put("success",true);
            resultMap.put("couponsId",coupons.getCouponsId());
            resultMap.put("couponsName",coupons.getCouponsName());
            resultMap.put("couponsPrice",coupons.getCouponsPrice());
        }else{
            resultMap.put("success",false);
            resultMap.put("msg","优惠卷不存在！");
        }

        String json = JSON.toJSONString(resultMap);

        return json;
    }

    //主页面添加
    @RequestMapping(value = "/indexCart", produces = {"json/html;charset=UTF-8"})
    @ResponseBody
    public String indexCart(int memberId, int goodsId,@RequestParam(defaultValue = "0") int goodsSpecs, HttpSession session){

        Goods goods = goodsService.findGoodsById(goodsId);
        Specs specs = goodsService.findSprcsById(goodsId,goodsSpecs);
        Cart cart = new Cart();
        cart.setMemberId(memberId);
        cart.setGoodsId(goodsId);
        cart.setSpecsId(specs.getSpecsId());
        cart.setGoodsImages(goods.getImage1());
        cart.setGoodsName(specs.getgName());
        cart.setUnitPrice(specs.getSpecsPrice());
        cart.setCartNumber(1);
        cart.setSpecsStock(specs.getSpecsStock());

        boolean result = cartService.addCar(cart);

        //获取购物车商品数量,和总价格
        int cartCount = cartService.getGoodsCount(cart.getMemberId()) ;
        double priceCount = cartService.CountUnitPrice(cart.getMemberId());
        session.setAttribute("cartCount", cartCount);
        session.setAttribute("priceCount",priceCount);

        Map resultMap = new HashMap();
        resultMap.put("success",result);
        resultMap.put("cartCount",cartCount);
        resultMap.put("priceCount",priceCount);

        String json = JSON.toJSONString(resultMap);

        return json;
    }

    //删除
    @RequestMapping(value = "/deleteCartId", produces = {"json/html;charset=UTF-8"})
    @ResponseBody
    public String deleteCartId(int[] id, HttpSession session) {
        boolean  result = cartService.daleteCart(id);

        //更新session里存储的购物车数量
        Member login = (Member)session.getAttribute("login") ;

        //获取购物车商品数量,和总价格
        int cartCount = cartService.getGoodsCount(login.getMemberId()) ;
        double priceCount = cartService.CountUnitPrice(login.getMemberId());
        session.setAttribute("cartCount", cartCount);
        session.setAttribute("priceCount",priceCount);

        System.out.println(result);
        Map resultMap = new HashMap();
        resultMap.put("success",result);
        resultMap.put("cartCount",cartCount);
        resultMap.put("priceCount",priceCount);

        String json = JSON.toJSONString(resultMap);

        return json ;
    }

    //修改
    @RequestMapping(value = "/updateCart", produces = {"json/html;charset=UTF-8"})
    @ResponseBody
    public String updateCart(int cartId, int cartNumber, HttpSession session) {
        boolean  result = cartService.updateCart(cartId,cartNumber);

        //更新session里存储的购物车数量
        Member login = (Member)session.getAttribute("login") ;

        //获取购物车商品数量,和总价格
        int cartCount = cartService.getGoodsCount(login.getMemberId()) ;
        double priceCount = cartService.CountUnitPrice(login.getMemberId());
        session.setAttribute("cartCount", cartCount);
        session.setAttribute("priceCount",priceCount);

        Map resultMap = new HashMap();
        resultMap.put("success",result);
        resultMap.put("cartCount",cartCount);
        resultMap.put("priceCount",priceCount);

        String json = JSON.toJSONString(resultMap);

        return json ;
    }

    @RequestMapping(value = "/addCart", produces = {"json/html;charset=UTF-8"})
    @ResponseBody
    public String addCart(Cart cart, HttpSession session){
        boolean result = cartService.addCar(cart);

        //获取购物车商品数量,和总价格
        int cartCount = cartService.getGoodsCount(cart.getMemberId()) ;
        double priceCount = cartService.CountUnitPrice(cart.getMemberId());
        session.setAttribute("cartCount", cartCount);
        session.setAttribute("priceCount",priceCount);

        Map resultMap = new HashMap();
        resultMap.put("success",result);
        resultMap.put("cartCount",cartCount);
        resultMap.put("priceCount",priceCount);

        String json = JSON.toJSONString(resultMap);

        return json;
    }

    /**
     * 购物车
     * @return
     */
    @RequestMapping("/cart")
    public String TestCart(HttpSession session, Model model){

        //获取当前登录用户
        Member member = (Member)session.getAttribute("login");
        int id = member.getMemberId();

        //根据会员ID获取对应的购物车数据
        List<Cart> cartList = cartService.getCartMemberById(id);
        model.addAttribute("cartList", cartList) ;

        return "front/system/system_cart";
    }
}
